﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
using WisPortal.Data;

namespace WisPortal.Controls
{
    public class QueryControl
    {
        public DataListControl DataList { get; set; }

        public QueryControl(DataListControl listControl)
        {
            DataList = listControl;
        }

        /// <summary>
        /// Get the html string by control configration
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public string GetControlHtml(XmlNode control)
        {
            string type = XmlHelper.GetAttributeValue(control, "type").ToLower().Trim(); 
            if (type.Equals("dropdownlist"))
            {
                return GenerateDropDownList(control);
            }
            else if (type.Equals("submit"))
            {
                return GenerateSubmitButton(control);
            }
            else
                return "";
        }

        protected string GenerateSubmitButton(XmlNode control)
        {
            return string.Format("<button class=\"btn\" type=\"submit\" name=\"{0}\" value=\"{1}\">{1}</button>",
                XmlHelper.GetAttributeValue(control,"name"),
                XmlHelper.GetAttributeValue(control,"title"));
        }

        protected string GenerateDropDownList(XmlNode control)
        {
            StringBuilder sb = new StringBuilder();
            string name = XmlHelper.GetAttributeValue(control, "name");
            string show_all = XmlHelper.GetAttributeValue(control, "show_all","false");
            string optionFormat = "<option value='{0}' title='{1}'>{1}</option>";
            sb.AppendFormat("<select name=\"{0}\">",name);
            
            XmlNode dataNode = control.SelectSingleNode("Data");
            if (dataNode != null)
            {
                if (show_all.Equals("true"))
                {
                    sb.AppendFormat(optionFormat, XmlHelper.GetAttributeValue(control, "all_value")
                            , UI.Localize(XmlHelper.GetAttributeValue(control, "all_text","All")));
                }
                string type = XmlHelper.GetAttributeValue(dataNode, "type").ToLower().Trim();
                if (type.Equals("inline"))
                {
                    foreach (XmlNode item in dataNode.SelectNodes("Item"))
                    {
                        sb.AppendFormat(optionFormat, XmlHelper.GetAttributeValue(item, "value")
                            , XmlHelper.GetAttributeValue(item, "text"));
                    }
                }
                else if(type.Equals("datasource"))
                {
                    string source = XmlHelper.GetAttributeValue(dataNode,"source");
                    string valueField = XmlHelper.GetAttributeValue(dataNode,"value_field");
                    string textField = XmlHelper.GetAttributeValue(dataNode,"text_field");    
                    DataSet ds = DataList.GetDataSource(source);
                    if (DB.HasData(ds))
                    {
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            sb.AppendFormat(optionFormat,row[valueField].ToString(), row[textField].ToString());
                        }
                    }
                }
            }

            sb.AppendLine("</select>");
            return sb.ToString();
        }
    }
}
